package yxy.game.pm2.command.capturestrategicpoints;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.CaptureStrategicPointsMessage;
import yxy.game.pm2.module.capturestrategicpoints.CaptureStrategicPointsHandle;
import yxy.game.pm2.module.capturestrategicpoints.CaptureStrategicPointsModule;

/**
 * @since 2022-08-15
 * @author wrs
 * @descrition 斩关夺隘布阵
 */
@MessageType(proto = Command.CaptureStrategicPointsFormat, classtype = CaptureStrategicPointsMessage.CaptureStrategicPointsFormatRequest.class)
public class CMD_CaptureStrategicPointsFormat extends Command<CaptureStrategicPointsMessage.CaptureStrategicPointsFormatRequest> {

    @Override
    public void execute(MessageSource messageSource, CaptureStrategicPointsMessage.CaptureStrategicPointsFormatRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        CaptureStrategicPointsMessage.CaptureStrategicPointsFormatResponse.Builder response = CaptureStrategicPointsMessage.CaptureStrategicPointsFormatResponse.newBuilder();

        try {
            CaptureStrategicPointsModule module = CaptureStrategicPointsModule.get(playerCode);
            if (module == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            CaptureStrategicPointsHandle handle = new CaptureStrategicPointsHandle();

            //todo 获取布阵信息以及武将信息
            Result result = handle.packFormatInfo(playerCode,module,response);

            send(messageSource,response,result);
        }catch (Exception e){
            e.printStackTrace();
            send(messageSource,response, Result.DataError);
        }

    }

    private void send(MessageSource messageSource, CaptureStrategicPointsMessage.CaptureStrategicPointsFormatResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
